/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.web.user.form;

import com.nus.application.web.base.form.BaseForm;
import com.nus.dataaccess.dao.role.RoleType;
import com.nus.dataaccess.dao.role.dao.RoleTypeDao;
import com.nus.dataaccess.dao.user.UserInfo;
import com.nus.dataaccess.dao.user.dao.UserInfoDao;
import com.nus.dataaccess.dao.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author robin.foe
 */
public class UserMaintenanceForm extends BaseForm{

    private List<UserInfo> userInfos;
    public List<UserInfo> getUserInfos() {return userInfos;}
    public void setUserInfos(List<UserInfo> userInfos) {this.userInfos = userInfos;}

    private UserInfo userInfo;
    public UserInfo getUserInfo() {return userInfo;}
    public void setUserInfo(UserInfo userInfo) {this.userInfo = userInfo;}

    private List<RoleType> roleTypes;
    public List<RoleType> getRoleTypes() {
        if (roleTypes == null)
            getAllRoleTypes();
        return roleTypes;
    }

    private List<Long> currentRoleTypes;
    public List<Long> getCurrentRoleTypes() {
        System.out.println("testing");
        List<RoleType> roleTypeTemp = userInfo.getUserAccount().getRoleTypes();
        System.out.println("Current size: "+roleTypeTemp.size());
        List<Long> ret = new ArrayList<Long>();
        for (RoleType roleType:roleTypeTemp) {
            ret.add(roleType.getId());
        }
        return ret;
    }


    public void getAllRoleTypes() {
        try {
            HibernateUtil.beginTransaction();
            roleTypes = RoleTypeDao.getInstance().findAll();
            HibernateUtil.commitTransaction();
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            e.printStackTrace();
        } finally {

            try {
                HibernateUtil.closeSession();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setUserInfoId(String id) {
        System.out.println("Calling set userinfo id = "+id);
        UserInfo userInfoTemp = UserInfoDao.getInstance().getUserInfoById(Long.parseLong(id));
        userInfo.setId(userInfoTemp.getId());
        userInfo.setEmail(userInfoTemp.getEmail());
        userInfo.setFullName(userInfoTemp.getFullName());
        userInfo.setGender(userInfoTemp.getGender());
        userInfo.setMobilePhone(userInfoTemp.getMobilePhone());
        userInfo.setOfficePhone(userInfoTemp.getOfficePhone());
        userInfo.setDob(userInfoTemp.getDob());

        userInfo.getUserAccount().setId(userInfoTemp.getUserAccount().getId());
        userInfo.getUserAccount().setPassword(userInfoTemp.getUserAccount().getPassword());
        userInfo.getUserAccount().setTheme(userInfoTemp.getUserAccount().getTheme());
        userInfo.getUserAccount().setUsername(userInfoTemp.getUserAccount().getUsername());

        userInfo.getUserAccount().setRoleTypes(userInfoTemp.getUserAccount().getRoleTypes());
        System.out.println("current size = "+userInfoTemp.getUserAccount().getRoleTypes().size());
    }

    public void setExistingId(String id) {
        if (id != null && id != "") {
            UserInfo userInfoTemp = UserInfoDao.getInstance().getUserInfoById(Long.parseLong(id));
            userInfo.setId(userInfoTemp.getId());
            userInfo.getUserAccount().setId(userInfoTemp.getUserAccount().getId());
            userInfo = (UserInfo) HibernateUtil.getCurrentSession().merge(userInfo);
        }
    }

    public UserMaintenanceForm(){
        this.userInfos = new ArrayList<UserInfo>();
        this.userInfo = new UserInfo();
    }

    @Override
    public List<UserInfo> getList() {return this.getUserInfos();}

}
